var Index = require('../app/controllers/index')
var User = require('../app/controllers/user')
var Movie = require('../app/controllers/movie')
var Catgory = require('../app/controllers/catgory')
var Comment = require('../app/controllers/comment')

module.exports = function (app) {
    // 预处理 user
    app.use(function (req, res, next) {
        // console.log('user in session : ' + JSON.stringify(req.session.user))
        var _user = req.session.user;

        app.locals.user = _user;

        return next()
    })

    // 首页
    app.get('/', Index.index)// 首页

    // 用户相关
    app.post('/user/signup', User.signup)// 注册接口
    app.post('/user/signin', User.signin)// 登录接口
    app.get('/signup', User.showSignup)// 注册
    app.get('/signin', User.showSignin)// 登录
    app.get('/logout', User.logout)// 退出
    app.get('/admin/user/list', User.signinRequired, User.adminRequired, User.list)// 用户列表页

    // 电影相关 
    app.get('/movie/:id', Movie.detail)// 电影详情页
    app.get('/admin/movie/new', User.signinRequired, User.adminRequired, Movie.new)// 后台录入页
    app.get('/admin/movie/update/:id', User.signinRequired, User.adminRequired, Movie.update)// 后台更新页

    app.post('/admin/movie/new', User.signinRequired, User.adminRequired, Movie.savePoster,Movie.save)// 后台录入提交
    app.get('/admin/movie/list', User.signinRequired, User.adminRequired, Movie.list)// 后台列表页
    app.delete('/admin/movie/list', User.signinRequired, User.adminRequired, Movie.del)// 电影删除

    app.post('/user/comment', User.signinRequired, Comment.save)// 评论

    app.get('/admin/catgory/new', User.signinRequired, User.adminRequired, Catgory.new)// 后台分类录入页
    app.post('/admin/catgory', User.signinRequired, User.adminRequired, Catgory.save)// 后台分类录入提交
    app.get('/admin/catgory/list', User.signinRequired, User.adminRequired, Catgory.list)// 后台分类列表页


    // 搜索
    app.get('/results',Index.search)
}
